/*
 * Copyright 2009 IT Mill Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package your.company;

import com.vaadin.*;
import com.vaadin.terminal.*;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.*;
import com.vaadin.ui.themes.*;
import org.apache.log4j.Logger;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyTrialVaadinApplication extends Application {

   private static final Logger log = Logger.getLogger(MyTrialVaadinApplication.class);
   private Window main;

   public void setMain() {
      main = new Window("My App");
      setMainWindow(main);
   }

   @Override
   public void init() {
      WebApplicationContext context = (WebApplicationContext) getContext();
      log.info("BaseDirectory: " + context.getBaseDirectory());
      //programaticMainLayout();
      customTopBottomLayout();
   }

   public void customTopBottomLayout() {
      setTheme("webhib");
      VerticalLayout rootLayout = new VerticalLayout();
      rootLayout.setSizeFull();
      rootLayout.setMargin(false);
      main = new Window("Custom Top/Buttom Layout Application", rootLayout);
      setMainWindow(main);

      rootLayout.addComponent(createTopLayoutCustom());

      Layout horizontalSplit = createPanelLayout();
      rootLayout.addComponent(horizontalSplit);
      rootLayout.setHeight(100, Sizeable.UNITS_PERCENTAGE);
      rootLayout.setExpandRatio(horizontalSplit, 1);

      rootLayout.addComponent(createBottomLayoutCustom());
   }

   private Layout createTopLayoutCustom() {
      CustomLayout topLayout = new CustomLayout("main_top");
      topLayout.setWidth(100, Layout.UNITS_PERCENTAGE);
      topLayout.setHeight(-1, Layout.UNITS_PIXELS);
      return topLayout;
   }

   private Layout createBottomLayoutCustom() {
      CustomLayout bottomLayout = new CustomLayout("main_bottom");
      bottomLayout.setWidth(100, Layout.UNITS_PERCENTAGE);
      bottomLayout.setHeight(-1, Layout.UNITS_PIXELS);
      bottomLayout.addStyleName("bordertop");
      return bottomLayout;
   }

   public void customMainLayout() {
      /** use a custom html layout for the application window at top level
       * disadvantage: bottom area cannot be positioned at bottom of window
       */
      setTheme("webhib");
      CustomLayout rootLayout = new CustomLayout("test_only/main");
      rootLayout.setSizeUndefined();
      //rootLayout.setMargin(false); not supported
      main = new Window("BaseLayout Application", rootLayout);
      setMainWindow(main);

      Layout horizontalSplit = createPanelLayout();
      horizontalSplit.setHeight("500px");
      rootLayout.addComponent(horizontalSplit, "panel");
      //rootLayout.setExpandRatio(horizontalSplit, 1); method does not exist
   }

   public void programaticMainLayout() {
      setTheme("webhib");
      VerticalLayout rootLayout = new VerticalLayout();
      rootLayout.setSizeFull();
      rootLayout.setMargin(false);
      main = new Window("BaseLayout Application", rootLayout);
      setMainWindow(main);

      rootLayout.addComponent(createTopLayout());

      Layout horizontalSplit = createPanelLayout();
      rootLayout.addComponent(horizontalSplit);
      rootLayout.setHeight(100, Sizeable.UNITS_PERCENTAGE);
      rootLayout.setExpandRatio(horizontalSplit, 1);

      rootLayout.addComponent(createBottomLayout());
   }

   private Layout createTopLayout() {
      GridLayout topLayout = new GridLayout(3, 1);
      topLayout.setWidth(100, Sizeable.UNITS_PERCENTAGE);
      topLayout.setMargin(true);
      topLayout.setSpacing(true);

      Label projectLabel = new Label("Project");
      /** relative to webapps directory, but only other web applications are usable otherwise
      the vaadin application catches the request (adaption of servlet mapping necessary */
      Embedded topCenter = new Embedded("Caption", new ExternalResource("manager/images/docs.gif"));
      Label topRight = new Label("Right away");
      topRight.setIcon(new ThemeResource("../runo/icons/16/globe.png"));
      projectLabel.addStyleName(Runo.LABEL_H1);
      projectLabel.setSizeUndefined();
      topCenter.setSizeUndefined();
      topRight.setSizeUndefined();
      topLayout.addComponent(projectLabel);
      topLayout.addComponent(topCenter);
      topLayout.addComponent(topRight);

      topLayout.setComponentAlignment(projectLabel, Alignment.MIDDLE_LEFT);
      topLayout.setComponentAlignment(topCenter, Alignment.MIDDLE_CENTER);
      topLayout.setComponentAlignment(topRight, Alignment.MIDDLE_RIGHT);

      return topLayout;
   }

   private Layout createPanelLayout() {
      HorizontalSplitPanel horizontalSplit = new HorizontalSplitPanel();
      horizontalSplit.setSplitPosition(200, HorizontalSplitPanel.UNITS_PIXELS);
      horizontalSplit.addStyleName(Runo.SPLITPANEL_SMALL);
      horizontalSplit.addStyleName("bordertop");
      horizontalSplit.addComponent(new Label("Left Pane"));
      horizontalSplit.addComponent(new Label("Right Pane"));
      return horizontalSplit;
   }

   private Layout createBottomLayout() {
      GridLayout bottomLayout = new GridLayout(3, 1);
      bottomLayout.setWidth(100, Sizeable.UNITS_PERCENTAGE);
      bottomLayout.setMargin(true);
      bottomLayout.setSpacing(true);

      Label bottomLeft = new Label("Bottom left");
      Label bottomCenter = new Label("Placeholder");
      Label bottomRight = new Label("Bottom right");
      bottomLeft.setSizeUndefined();
      bottomCenter.setSizeUndefined();
      bottomRight.setSizeUndefined();
      bottomLayout.addComponent(bottomLeft);
      bottomLayout.addComponent(bottomCenter);
      bottomLayout.addComponent(bottomRight);

      bottomLayout.setComponentAlignment(bottomLeft, Alignment.MIDDLE_LEFT);
      bottomLayout.setComponentAlignment(bottomCenter, Alignment.MIDDLE_CENTER);
      bottomLayout.setComponentAlignment(bottomRight, Alignment.MIDDLE_RIGHT);

      bottomLayout.addStyleName("bordertop");

      return bottomLayout;
   }

   //----------------------------------------------------------------------------
   public void accordion() {
      // Create the Accordion.
      Accordion accordion = new Accordion();
      // Have it take all space available in the layout.
      accordion.setSizeFull();
      // Some components to put in the Accordion.
      Label l1 = new Label("There are no previously saved actions.");
      Label l2 = new Label("There are no saved notes.");
      Label l3 = new Label("There are currently no issues.");
      l1.setSizeFull();
      l2.setSizeFull();
      l3.setSizeFull();
      // Add the components as tabs in the Accordion.
      accordion.addTab(l1, "Saved actions", null);
      accordion.addTab(l2, "Notes", null);
      accordion.addTab(l3, "Issues", null);

      VerticalLayout panelLayout = new VerticalLayout();
      // Trim its layout to allow the Accordion take all space.
      panelLayout.setSizeFull();
      // A container for the Accordion.
      Panel panel = new Panel("Tasks", panelLayout);
      panel.setWidth("300px");
      panel.setHeight("200px");
      panel.addComponent(accordion);
      // Trim its layout to allow the Accordion take all space.
      //panel.getContent().setSizeFull();
      //panel.getLayout().setMargin(false);
      main.addComponent(panel);
   }

   public void splitPanelSimple() {
      // Have a window with a SplitPanel.
      final Window window = new Window("Window with a Light Panel");
      window.setWidth("400px");
      window.setHeight("200px");
      final HorizontalSplitPanel splitter = new HorizontalSplitPanel();
      window.setContent(splitter);
      // Create a panel with a caption.
      //final Panel light = new Panel("Light Panel");
      final Panel light = new Panel();
      light.setSizeFull();
      // The "light" style is a predefined style without borders
      light.addStyleName(Runo.PANEL_LIGHT);
      light.addComponent(new Label("The light Panel has no borders."));
      light.getLayout().setMargin(true);
      // The Panel will act as a "caption" of the left panel
      // in the SplitPanel.
      splitter.addComponent(light);
      //splitter.setSplitPosition(250, Sizeable.UNITS_PIXELS);
      splitter.setSplitPosition(30, Sizeable.UNITS_PERCENTAGE);
      main.addWindow(window);
   }

   public void panel() {
      // Create a panel with a caption.
      final Panel panel = new Panel("Contact Information");
      panel.addStyleName(Runo.PANEL_LIGHT);
      // The width of a Panel is 100% by default, make it
      // shrink to fit the contents.
      panel.setWidth(Sizeable.SIZE_UNDEFINED, 0);
      // Create a layout inside the panel
      final FormLayout form = new FormLayout();
      // Have some margin around it.
      form.setMargin(true);
      // Add some components
      form.addComponent(new TextField("Name"));
      form.addComponent(new TextField("Email"));
      // Set the layout as the root layout of the panel
      panel.setContent(form);

      main.addComponent(panel);
   }

   public void example2() {
      VerticalLayout root = new VerticalLayout();
      main.setContent(root);
      root.addComponent(new Label("Project Title"));
      HorizontalLayout bottom = new HorizontalLayout();
      root.addComponent(bottom);
      bottom.addComponent(new Tree("The Planets"));
      VerticalLayout panel = new VerticalLayout();
      bottom.addComponent(panel);
      log.info("width: " + panel.getWidth());
      log.info("height: " + panel.getHeight());
      panel.setHeight("250px");
      panel.addComponent(new TextField("Name"));
      panel.addComponent(new TextField("Stree"));
      TextField expand = new TextField("Zip");
      panel.addComponent(expand);
      expand.setWidth("100%");
      panel.setExpandRatio(expand, 1.0f);
   }

   public void example1() {
      Button button = new Button("Click Me");
      button.addListener(new Button.ClickListener() {

         public void buttonClick(ClickEvent event) {
            main.addComponent(new Label("Thank you for clicking"));
         }
      });
      main.addComponent(button);
   }
}
